package org.dromara.system.runner;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.dromara.system.service.ICommonService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


/**
 * 注：如果同时implements   ApplicationListener和CommandLineRunner两个接口，ApplicationRunner接口的方法先执行,CommandLineRunner后执行;
 */
@Component
@Order(value = 5)
@Slf4j
public class CommunityRunner implements ApplicationRunner, CommandLineRunner {

    @Resource
    private ICommonService commonService;


    /**
     * 是否开启elasticsearch初始化
     */
    @Value("${init.cacheData}")
    private boolean initElasticsearch;


    @Override
    public void run(ApplicationArguments args) {
        if (initElasticsearch) {
            log.info("服务启动MyRunner, ApplicationRunner执行启动加载任务...");
            commonService.initCommunity();
            log.info("初始化执行完了哟");
        }
    }


    @Override
    public void run(String... args) throws Exception {
        if (initElasticsearch) {
            log.info("志愿服务统计缓存开始");
            commonService.initZhiYuanStatistic();
            log.info("志愿服务统计缓存结束");
            log.info("志愿积分统计缓存开始");
            commonService.getZhiYuanJiFen();
            log.info("志愿积分统计缓存结束");
        }
    }
}
